package deleteface.bank.jqsoft.cn.deleteface.http;

import android.text.TextUtils;
import android.util.Log;


import deleteface.bank.jqsoft.cn.deleteface.setip.Util;
import okhttp3.ResponseBody;
import rx.Subscriber;

/**
 * @author yedong
 * @date 2019/7/19
 * 自定义订阅者 封装网络请求回调解析处理
 */
public abstract class MySubscriber<T> extends Subscriber<T> {

    public MySubscriber() {
    }

    @Override
    public void onCompleted() {

    }

    @Override
    public void onError(Throwable throwable) {
        onError(throwable.getMessage());
    }

    @Override
    public void onNext(T t) {
        try {
            ResponseBody body = (ResponseBody) t;
            String json = Util.DecodeXmlJosn(body);
            Log.e("Net", "response:" + json);
            if (TextUtils.isEmpty(json)) {
                onError("解析失败");
            } else {
                onSuccess(json);
            }
        } catch (Exception e) {
            onError(e.getMessage());
        }
    }

    /**
     * 网络请求失败
     *
     * @param msg 提示信息
     */
    public abstract void onError(String msg);

    /**
     * 网络请求成功
     *
     * @param json 数据
     */
    public abstract void onSuccess(String json);

}
